package com.cn.lp.feign.param;

import com.cn.lp.common.utils.BlankAide;
import com.cn.lp.reflect.reflect.GClass;
import com.cn.lp.reflect.reflect.GPropertyAccessor;
import com.cn.lp.reflect.reflect.utils.GSsistAide;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public abstract class RequestParamBean implements RequestParam {

    private Map<String, Object> dataMap = new HashMap<>();

    private Class<?> clazz;

    public <T extends RequestParamBean> RequestParamBean(Class<T> clazz) {
        this.clazz = clazz;
    }

    @Override
    public void initMap() {
        try {
            Field[] declaredFields = clazz.getDeclaredFields();
            GClass gClass = GSsistAide.getGClass(clazz);
            Map<String, GPropertyAccessor> accessorMap = gClass.getAccessorMap();
            if(BlankAide.isNotBlank(accessorMap)) {
                for (Field declaredField : declaredFields) {
                    GPropertyAccessor gPropertyAccessor = accessorMap.get(declaredField.getName());
                    dataMap.put(declaredField.getName(),gPropertyAccessor.getPropertyValue(this));
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int size() {
        return dataMap.size();
    }

    @Override
    public boolean isEmpty() {
        return dataMap.isEmpty();
    }

    @Override
    public boolean containsKey(Object key) {
        return dataMap.containsKey(key);
    }

    @Override
    public boolean containsValue(Object value) {
        return dataMap.containsValue(value);
    }

    @Override
    public Object get(Object key) {
        return dataMap.get(key);
    }

    @Override
    public Object put(String key, Object value) {
        return dataMap.put(key, value);
    }

    @Override
    public Object remove(Object key) {
        return dataMap.remove(key);
    }

    @Override
    public void putAll(Map<? extends String, ?> m) {
        dataMap.putAll(m);
    }

    @Override
    public void clear() {
        dataMap.clear();
    }

    @Override
    public Set<String> keySet() {
        return dataMap.keySet();
    }

    @Override
    public Collection<Object> values() {
        return dataMap.values();
    }

    @Override
    public Set<Entry<String, Object>> entrySet() {
        return dataMap.entrySet();
    }

}
